@echo off
cd /d %~dp0

echo Sonic Forever Setup


if exist Data\Game\GameConfig.bin (goto :dataq) else (goto :data)



:dataq
	echo.
	echo There is already a data folder here.
	choice  /m "Do you want to overwrite it?"
	IF %ERRORLEVEL% EQU 1 rd /q /s Data
	IF %ERRORLEVEL% EQU 1 goto :data
goto :afterdata




:data
	echo.

	echo Extracting data from APK file...
	:: Extract the data from the app
	7za e *.apk Data.rsdk.xmf -r -aoa > nul

	:: Check that the extraction was successful
	if exist Data.rsdk.xmf (echo Extraction successful) else (goto :nodatarsdk)
	echo.
	echo Unpacking data file...


	:: remove the .xmf at the end of the data file
	ren Data.rsdk.xmf Data.rsdk

	::Get out retrun
	7za e data.7z  Retrun\retrun.exe > nul

	7za e data.7z  Retrun\RSDKv4FileList.txt > nul


	:: run the regular Sonic 1 unpack script
	retrun x Data.rsdk -3 -L=RSDKv4FileList.txt

	::finished with Data.rsdk, delete it
	del Data.rsdk

	
	::finished with Retrun, delete it
	rd /q /s ByteCode
	del retrun.exe
	del RSDKv4FileList.txt


	if exist Data\Game\GameConfig.bin (echo Data unpacked) else (goto :unpackfailed)
	echo.
	echo Patching data...
	
	7za x data.7z Data\* -r -aoa > nul

	if exist Data\Stages\MainMenu\Act1.bin (echo Data patched) else (goto :patchfailed)

goto :afterdata



:afterdata
if exist Scripts\Players\PlayerObject.txt (goto :scriptsq) else (goto :scripts)



:scriptsq
echo.
echo There is already a scripts folder here.
choice  /m "Do you want to overwrite it?"
IF %ERRORLEVEL% EQU 1 rd /q /s Scripts
IF %ERRORLEVEL% EQU 1 goto :scripts
goto :afterscripts



:scripts
echo.
echo Unpacking Scripts...
7za x data.7z Scripts\* -r > nul
if exist Scripts\Players\PlayerObject.txt (echo Scripts unpacked.) else (goto :scriptsfailed)
goto :afterscripts



:afterscripts
echo.
echo Unpacking game executable...
if exist SDL2.dll (del SDL2.dll)
7za e data.7z SDL2.dll -r > nul
if exist SonicForever.exe (del SonicForever.exe)
7za e data.7z SonicForever.exe -r > nul
::if not exist settings.ini (7za e data.7z settings.ini -r > nul)

if exist Mods\Scaffolded_Star_Light\NUL (rmdir /s /q Mods\Scaffolded_Star_Light)
7za x data.7z Mods\* -r > nul

echo Setup Complete!
PING localhost -n 3 >NUL
exit





::Error Handling

:nodatarsdk
echo.
echo The data file could not be extracted.
echo Please make sure you have provided a copy of "Sonic the Hedgehog Classic".
echo Press any key to exit.
Pause >nul
exit



:unpackfailed
echo.
echo The data file could not be unpacked. There may be an issue with your Sonic Forever download.
echo Press any key to exit.
Pause >nul
exit



:patchfailed
echo.
echo The data patch could not be applied. There may be an issue with your Sonic Forever download.
echo Press any key to exit.
Pause >nul
exit



:scriptsfailed
echo.
echo The game scripts could not be unpacked. There may be an issue with your Sonic Forever download.
echo Press any key to exit.
Pause >nul
exit